﻿
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Filters;
using Serilog.Formatting.Json;
using Serilog.Sinks.MSSqlServer;

namespace SBlogCore.Common.LogExtend
{
    [SkipScan]
    public static class SerilogExtensions
    {
        public static void AddSerilogSetUp(this WebApplicationBuilder builder)
        {
            var loggingBuilder = builder.Logging;
            loggingBuilder.ClearProviders();//删除其他的关于日志的配置
            var connectionString = "Server=121.4.70.131;Database=SBlogCore;User ID=sa;Password=SQLSEVER960714hh@123";
            var sinkOptions = new MSSqlServerSinkOptions()
            {
                TableName = "SysLogging",
                AutoCreateSqlTable = true//是否自动创建表
            };
            var jobOptions = new MSSqlServerSinkOptions()
            {
                TableName = "SysOpenJobLogging",
                AutoCreateSqlTable = true//是否自动创建表
            };
            //自定义列
            //var columnOptions = new ColumnOptions
            //{
            //    AdditionalColumns = new List<SqlColumn>()
            //    {
            //        new SqlColumn {DataType = SqlDbType.NVarChar, ColumnName = "User"},
            //        new SqlColumn {DataType = SqlDbType.NVarChar, ColumnName = "Class"},
            //    }
            //};
            Log.Logger = new LoggerConfiguration()
                //.ReadFrom.Configuration(builder.Configuration)
                .MinimumLevel.Debug()
                .Enrich.FromLogContext()
                .WriteTo.Console() //控制台打印
                .WriteTo.File(new JsonFormatter(), "log/error/errorlog.txt",
                            rollingInterval: RollingInterval.Day,
                            restrictedToMinimumLevel: LogEventLevel.Error,
                            rollOnFileSizeLimit: true,
                            fileSizeLimitBytes: 102400)//日志分页
                .WriteTo.File(new JsonFormatter(), "log/warn/warnlog.txt",
                            rollingInterval: RollingInterval.Day,
                            restrictedToMinimumLevel: LogEventLevel.Warning,
                            rollOnFileSizeLimit: true,
                            fileSizeLimitBytes: 102400)
                .WriteTo.File(new JsonFormatter(), "log/info/infolog.txt",
                            rollingInterval: RollingInterval.Day,
                            restrictedToMinimumLevel: LogEventLevel.Information,
                            rollOnFileSizeLimit: true,
                            fileSizeLimitBytes: 102400)
                .WriteTo.MSSqlServer(
                    connectionString: connectionString,
                    sinkOptions: sinkOptions, 
                    restrictedToMinimumLevel: LogEventLevel.Error, 
                    logEventFormatter: new JsonFormatter()
                    )
                .WriteTo.Logger(lc=>lc
                    .Filter.ByIncludingOnly(Matching.WithProperty("OpenJob"))//通过 Serilog.Log.ForContext 来自定义日志
                    .WriteTo.MSSqlServer(
                        connectionString: connectionString, 
                        sinkOptions: jobOptions, 
                        logEventFormatter: new JsonFormatter()
                        )
                )
                .CreateLogger();
            loggingBuilder.AddSerilog();
            //builder.Logging.SetMinimumLevel(LogLevel.Debug); 
            //30840
            //102400
        }
    }
}
